在mac上安装mongodb有很多方式,HomeBrew 安装 MongoDB是最快速简洁的,也方便后面的维护,这次主要记录使用brew安装的方式

更新Homebrew的package源,在Mac的终端中输入:

$ brew update

开始安装MongoDB

$ brew install mongodb

然后等待MongoDB下载完成。

==> Downloading https://homebrew.bintray.com/bottles/mongodb-3.4.7.sierra.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring mongodb-3.4.7.sierra.bottle.1.tar.gz
==> Caveats
To have launchd start mongodb now and restart at login:
  brew services start mongodb
Or, if you don't want/need a background service you can just run:
  mongod --config /usr/local/etc/mongod.conf
==> Summary
🍺  /usr/local/Cellar/mongodb/3.4.7: 19 files, 281.9MB

启动MongoDB

上面最后提示的直接启动MongoDb的方法.

mongod —config /usr/local/etc/mongod.conf

后台方式启动

nohup mongod —config /usr/local/etc/mongod.conf &

关闭MongoDB服务

mongo shell中使用 db.shutdownServer() 方法来关闭 mongod ,如下所示:

use admin
db.shutdownServer()

对于启用 authorization 的系统,通过验证登陆到 admin 数据库,或者在为开启验证的情况下经由本地主机的系统接口登陆到 admin 数据库时,用户可以仅仅通过发送 db.shutdownServer() 来关闭实例。

For systems with authorization enabled, users may only issue db.shutdownServer() when authenticated to the admin database or via the localhost interface on systems without authentication enabled.

使用 --shutdown

从Linux命令行关闭 mongod ,可以使用 –shutdown 选项 如下所示:

1
2
mongod --shutdown

使用 CTRL-C

当正在运行一个交互模式 (i.e. without –fork) 的 mongod 实例时,发送 Control-C 来进行正常关闭。

使用 kill

From the Linux command line, shut down a specific mongod instance using one of the following commands:

1
2
kill <mongod process ID>
kill -2 <mongod process ID>

连接到MongoDb,直接在命令行使用mongo连接:

$ mongo

MongoDB shell version v3.4.7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.7
Server has startup warnings:
2017-08-24T09:47:55.554+0800 I CONTROL  [initandlisten]
2017-08-24T09:47:55.554+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-08-24T09:47:55.554+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-08-24T09:47:55.554+0800 I CONTROL  [initandlisten]
>